﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;
using BusinessObjects.BO_GetObjects;

public partial class Administrator_Default : System.Web.UI.Page
{
    private DataTable objDT;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            string username = String.Empty;
            int admin_id = -1;
            if (Session["admin"] == null || Session["admin"] == "")
            {
                Response.Redirect("login.aspx", false);
                return;
            }
            else
            {
                DataTable user = (DataTable)Session["admin"];
                if (user != null && user.Rows.Count > 0)
                {
                    ltrUserName.Text = user.Rows[0]["USER_NAME"].ToString();
                    username = user.Rows[0]["USER_NAME"].ToString();
                    admin_id = Convert.ToInt32(user.Rows[0]["ADMIN_ID"]);
                }
            }
            if (!IsPostBack)
            {
                getParentSec_Menu();
                //if (getMessages() > 0)
                //    lbtMessages.Text = "Messages(" + getMessages() + ")";
                //else
                //    liMessages.Attributes.Clear();
            }
            if (Request["module"] != null && Request["error"] == null)
            {

                string module = Request["module"].ToString();
                if (module.Equals("") || module.Equals("controlpanel", StringComparison.OrdinalIgnoreCase) || module.Equals("logout", StringComparison.OrdinalIgnoreCase) || module.Equals("changepassword", StringComparison.OrdinalIgnoreCase))
                {
                    loadControls(module);
                    return;
                }
                else
                {
                    DataTable objDT = BO_Sec_Admin_Modules.SearchByName(module);
                    if (objDT != null && objDT.Rows.Count > 0)
                    {
                        string query = Request.QueryString.ToString().Substring(7, Request.QueryString.ToString().Length - 7);
                        if (!BO_AdminUsers.IsInRole(username, query))
                        {
                            if (!BO_AdminUsers.IsInRole(username, module))
                            {
                                Response.Redirect("Default.aspx?module=controlpanel&error=" + module, false);
                                return;
                            }
                            loadControls(Request["module"].ToString());
                        }
                        else loadControls(module);
                        if (objDT.Rows.Count > 0)
                            BO_Sec_Syslog.Insert(Convert.ToInt32(objDT.Rows[0]["ADMIN_MODULE_ID"]), admin_id, Request.ServerVariables["REMOTE_ADDR"].ToString(), DateTime.Now, true);
                    }
                    else
                    {
                        loadControls("controlpanel");
                        return;
                    }
                }
            }
            else loadControls("controlpanel");
        }
        catch (Exception ex) { loadControls("controlpanel"); }
        finally { }
    }
    private int getLeftDay()
    {
        try
        {
            DataSet dsExpires = new DataSet("Expires");
            string fileName = Server.MapPath("../upload/ads/Expires.xml");
            dsExpires.ReadXml(fileName);
            if (dsExpires != null && dsExpires.Tables.Count > 0)
                return Convert.ToInt32(dsExpires.Tables[0].Rows[0][0]);
            else
                return 30;
        }
        catch { return 30; }
    }
    private int getExpiresAds(int count)
    {
        int left_Day = getLeftDay();
        DataTable objDT = BO_Ads_Picture.Expires(left_Day);
        if (objDT != null && objDT.Rows.Count > 0)
            count += 1;
        return count;
    }
    private int getMessages()
    {
        int count = 0;
        count = getExpiresAds(count);
        return count;
    }
    protected void loadControls(string controlName)
    {
        plhdMain.Controls.Clear();
        string test = "../controls/admin/" + controlName + ".ascx";
        UserControl controls = (UserControl)Page.LoadControl("../controls/admin/" + controlName + ".ascx");
        plhdMain.Controls.Add(controls);
    }
    protected void lbtMessages_Click(object sender, EventArgs e)
    {
        liMessages.Attributes.Clear();
        Response.Redirect("Default.aspx?module=Messages");
    }
    //Menu
    protected void rptMenu_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        try
        {
            HtmlGenericControl spanIcon = (HtmlGenericControl)e.Item.FindControl("spanIcon");
            HiddenField hdfThumIcon = e.Item.FindControl("hdfThumIcon") as HiddenField;
            HiddenField hdfDescription = e.Item.FindControl("hdfDescription") as HiddenField;
            if (hdfThumIcon.Value.Trim().Equals(String.Empty))
            {
                spanIcon.Attributes.Add("class", "normal");
                spanIcon.InnerHtml = hdfDescription.Value;
                HtmlGenericControl spanName = (HtmlGenericControl)e.Item.FindControl("spanName");
                spanName.Visible = false;
            }
        }
        catch (Exception ex) { throw ex; }
        finally { }
    }
    protected void rptChildMenu1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        try
        {
            HiddenField hdfAdmin_module_id = (HiddenField)e.Item.FindControl("hdfAdmin_module_id");
            objDT = getChildMenus(Convert.ToInt32(hdfAdmin_module_id.Value));
            if (objDT != null && objDT.Rows.Count > 0)
            {
                HtmlAnchor aHasChild = (HtmlAnchor)e.Item.FindControl("aHasChild");
                aHasChild.Attributes.Add("class", "childs");
            }
            else
            {
                HtmlGenericControl ulChild2 = (HtmlGenericControl)e.Item.FindControl("ulChild2");
                ulChild2.Visible = false;
                Repeater rptChild2 = (Repeater)e.Item.FindControl("rptChild2");
                rptChild2.Visible = false;
            }
            if (e.Item.ItemIndex + 1 == objDT.Rows.Count)
            {
                HtmlGenericControl liChild1 = (HtmlGenericControl)e.Item.FindControl("liChild1");
                liChild1.Attributes.Add("class", "last");
            }
        }
        catch (Exception ex) { throw ex; }
        finally { }
    }
    protected void rptChild2_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        try
        {
            objDT = getChildMenus(Convert.ToInt32(Eval("admin_module_id")));
            if (objDT != null && objDT.Rows.Count > 0)
            {
                if (e.Item.ItemIndex + 1 == objDT.Rows.Count)
                {
                    HtmlGenericControl liChild2 = (HtmlGenericControl)e.Item.FindControl("liChild2");
                    liChild2.Attributes.Add("class", "last");
                }
            }
        }
        catch (Exception ex) { throw ex; }
        finally { }
    }
    protected void getParentSec_Menu()
    {
        try
        {
            DataTable objDT = BO_Sec_Admin_Modules.SelectParent(1, 0);
            if (objDT.Rows.Count > 0)
            {
                rptMenu.DataSource = objDT;
                rptMenu.DataBind();
            }
        }
        catch (Exception ex) { throw ex; }
        finally { }
    }
    protected DataTable getChildMenus(int parent_menu_id)
    {
        try
        {
            DataTable objDTs = BO_Sec_Admin_Modules.SelectParent(1, parent_menu_id);
            return objDTs;
        }
        catch (Exception ex) { return null; throw ex; }
        finally { }
    }
    protected string Get_Menu_Link(string link)
    {
        if (link.Equals("#"))
        {
            return "#";
        }
        else
        {
            return "Default.aspx?module=" + link;
        }
    }
}